/**
 * Created by zhengqiang.liu on 2018/6/21.
 */
import * as activityService from '../services/activity';

export default {
    namespace: 'activity',
    state: {
        list: [],
        total: null,
        selectedRowKeys:[]
    },
    reducers: {
        save(state, { payload: { data: list, total } }) {
            return { ...state, list, total };
        },
        selected(state, { payload: {selectedRowKeys} }) {
            return { ...state, selectedRowKeys };
        },
    },
    effects: {
        *fetch({ payload: { page } }, { call, put }) {
            const { data } = yield call(activityService.fetch, { page });
            yield put({ type: 'save', payload: { data,total:2}});
        },
        *remove({ payload: id }, { call, put, select }) {
            yield call(activityService.remove, id);
            const page = yield select(state => state.users.page);
            yield put({ type: 'fetch', payload: { page } });
        },
        *patch({ payload: { id, values } }, { call, put, select }) {
            yield call(activityService.patch, id, values);
            const page = yield select(state => state.users.page);
            yield put({ type: 'fetch', payload: { page } });
        },
        *create({ payload: values }, { call, put, select }) {
              yield call(activityService.create, values);
                const page = yield select(state => state.users.page);
              yield put({ type: 'fetch', payload: { page } });
               },
    },
    subscriptions: {
        setup({ dispatch, history }) {
            return history.listen(({ pathname, query }) => {
                if (pathname === '/activity') {
                    dispatch({ type: 'fetch', payload: query });
                }
            });
        },
    },
};